'use strict'

define(['jquery', 'bootstrap', 'datetimepicker', 'datetimepickerCN', 'select2', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
        
    init: function(data) {
        this.initPage(data.coachview, data.beginDate, data.weekOffset, data.coachId);
        this.edit();
        this.batchSchedule();
        this.batchBusinessCalendar();
        this.exportSchedule();
        this.editAnnualLeave();
        this.addExperience();
        this.cancelExperience();
    },
    
    initPage: function(coachview, beginDate, weekOffset, coachId){
        
        var coachview = coachview;
        var beginDate = beginDate;
        var weekOffset = weekOffset;
        var coachId = coachId;
        
        $('select[name="site"]').select2({
          minimumResultsForSearch : -1,
          allowClear : true,
          width: '150'
        });

        $('select[name="site"]').change(function(){ 
          
          
          var site = $(this).children('option:selected').val();
          
          window.location.href = window.__CTX__.root + generateUrl(site, coachview, beginDate, weekOffset, coachId);
        })
        
        function generateUrl(site, coachview, beginDate, weekOffset, coachId) {
          var url = "/schedules?site=" + site;
          if(coachview) {
            url = url + "&coachview=" + coachview;
          }
          
          if(beginDate) {
            url = url + "&beginDate=" + beginDate;
          }
          
          if(weekOffset) {
            url = url + "&weekOffset=" + weekOffset;
          }
          
          if(coachId) {
            url = url + "&coachId=" + coachId;
          }
          
          return url;
        }
        
        $(function(){
            $('.init-button').removeClass('hide')
        })
        
        /*$(".course-calendar > div").each(function() {
            $(".course-calendar").removeClass("hide");
              var begin = ($(this).data("begin")).toString();
              var end = $(this).data("end");
              
              var beginOffset = $("."+ begin).offset();
              var endOffset = $("."+ end).offset();
              var width = endOffset.left - beginOffset.left;
              $(this).css('width',  width + "px").offset(beginOffset)
          })*/
    },
    
    edit: function() {
        var that = this
        $(document).on('shown.bs.modal', '#schedule-edit', function(e){
            $('select[name="type"]').change(function() {
                var type = $(this).val();
                if(type == 'business') {
                    $('.business-hour').removeClass('hide');
                    $('.duty-hour').addClass('hide');
                    $('.experience-hour').addClass('hide');
                    $('.conference-hour').addClass('hide');
                    $('.training-hour').addClass('hide')
                } else if(type == 'duty') {
                    $('.duty-hour').removeClass('hide');
                    $('.business-hour').addClass('hide');
                    $('.experience-hour').addClass('hide');
                    $('.conference-hour').addClass('hide');
                    $('.training-hour').addClass('hide')
                } else if(type == 'experience') {
                    $('.experience-hour').removeClass('hide');
                    $('.duty-hour').addClass('hide');
                    $('.business-hour').addClass('hide');
                    $('.conference-hour').addClass('hide');
                    $('.training-hour').addClass('hide')
                } else if(type == 'conference') {
                    $('.conference-hour').removeClass('hide');
                    $('.duty-hour').addClass('hide');
                    $('.business-hour').addClass('hide');
                    $('.experience-hour').addClass('hide');
                    $('.training-hour').addClass('hide')
                } else if(type == 'training') {
                    $('.training-hour').removeClass('hide');
                    $('.duty-hour').addClass('hide');
                    $('.business-hour').addClass('hide');
                    $('.experience-hour').addClass('hide');
                    $('.conference-hour').addClass('hide')
                }
            })
            
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
      },
      
      batchSchedule: function() {
          var that = this
          $(document).on('shown.bs.modal', '#batch-schedule', function(e){
              
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
          })
        },
        
        batchBusinessCalendar: function() {
              var that = this
              $(document).on('shown.bs.modal', '#batch-business-calendar', function(e){
                  
                  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                  
                  var $form = $(e.currentTarget).find("form")                          //bind shiftform
                  $form.shiftform({
                    beforeSubmit : function(el) {
                      $form.trigger("validate.ajax.submit");
                      return $.bt_validate.result[$.bt_validate.form_id]
                    }
                  })
              })
            },
        exportSchedule: function() {
              var that = this
              $(document).on('shown.bs.modal', '#export-schedule', function(e){
                  
                  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                  
                  $('button[type="submit"').click(function() {
                      $(e.currentTarget).find("form").trigger("validate.ajax.submit");
                      $.bt_validate.result[$.bt_validate.form_id]
                  })
                  
                  
              })
            },
      
        editAnnualLeave: function() {
              var that = this
              $(document).on('shown.bs.modal', '#annual-leave', function(e){
                  
                  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                  
                  var $form = $(e.currentTarget).find("form")                          //bind shiftform
                  $form.shiftform({
                    beforeSubmit : function(el) {
                      $form.trigger("validate.ajax.submit");
                      return $.bt_validate.result[$.bt_validate.form_id]
                    }
                  })
              })
          },
          
          addExperience: function() {
              var that = this;
              $(document).on('shown.bs.modal', '#experience-add', function(e){
                  $('select[name="site"]').select2({
                      minimumResultsForSearch : -1,
                      allowClear : false,
                      width: '100%'
                  });
                  
                  $('.select2').select2({
                      placeholder : '请选择',
                      minimumResultsForSearch : -1,
                      allowClear : true,
                      width: '100%'
                  });
                  
                  $('.date').datetimepicker({
                      format: 'yyyy-mm-dd',
                      autoclose: true,
                      language: 'zh-CN',
                      todayBtn: 'linked',
                      todayHighlight: true,
                      pickerPosition: 'bottom-right',
                      minView: 2,
                      startDate: '${today}'
                  });
                  
                  $('select[name="site"]').on('change', function() {
                      var siteId = $(this).val();
                      var coachId = $('input[name="coach"]').val();
                      var date = $('input[name="date"]').val();
                      
                      $.ajax({
                          type: 'POST',
                          url: window.__CTX__.root + '/schedules/coach-experience-hour',
                          data: {
                              'coach': coachId,
                              'date': date,
                              'site': siteId
                          },
                          success: function(res) {
                              var hourSelect = '<select name="experienceHour" class="form-control select2" data-validate="required">'
                                               + '<option></option>';
                              res = JSON.parse(res);
                              if(res.data.amount > 0) {
                                  for(var index in res.data.hours) {
                                      hourSelect = hourSelect + '<option value="' + res.data.hours[index].begin + '">' + res.data.hours[index].begin + '~' + res.data.hours[index].end + '</option>';
                                  }
                              }
                              hourSelect = hourSelect + '</select>';
                              $('.hour-item').html(hourSelect);
                              
                              $('select[name="experienceHour"]').select2({
                                  placeholder : '请选择',
                                  minimumResultsForSearch : -1,
                                  allowClear : true
                              });
                          }
                      });
                      
                      
                  });
                  
                  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                    
                    var $form = $(e.currentTarget).find("form")                          //bind shiftform
                    $form.shiftform({
                      beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                      }
                    })
                })
            },
            
            cancelExperience: function() {
                  var that = this
                  $(document).on('shown.bs.modal', '#experience-cancel', function(e){
                      
                      $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                      
                      var $form = $(e.currentTarget).find("form")                          //bind shiftform
                      $form.shiftform({
                        beforeSubmit : function(el) {
                          $form.trigger("validate.ajax.submit");
                          return $.bt_validate.result[$.bt_validate.form_id]
                        }
                      })
                  })
                }
  }
})
